﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class CTVATDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public int CheckInvoiceLock(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoiceLockedVAT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[3].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int CheckInvoiceStatus(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoiceStatusVAT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[3].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public bool Delete(string ps_UserName, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_SttV", po_KeyCode), new SqlParameter("p_UserName", ps_UserName), new SqlParameter() };
            values[2].Direction = ParameterDirection.ReturnValue;
            values[2].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtVAT_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[2].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public DataRow GetDataRow(string ps_InvoiceOrder)
        {
            DataRow row;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_SttV", ps_InvoiceOrder) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtVAT_One_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                if (dataTable.Rows.Count == 0)
                {
                    dataTable.Rows.Add(dataTable.NewRow());
                    dataTable.AcceptChanges();
                }
                row = dataTable.Rows[0];
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return row;
        }

        public DataTable GetTableByMaster(string ps_MasterInvoiceOrder)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_MasterInvoiceOrder) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtVAT_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetTableByType(string ps_ActiveTransaction)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Loai_Ps", ps_ActiveTransaction) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtVATH_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public void Lock(string ps_UserName, object ps_InvoiceOrder, bool pb_Locked)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Locked", pb_Locked) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UpdateLockedVAT";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }

        public bool NumberExisted(string ps_Subsidiary, bool pb_CheckNewRow, string ps_InvoiceOrder, object ps_So_Ct0, object ps_So_Seri0)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Moi_Sua", pb_CheckNewRow), new SqlParameter("p_SttV", ps_InvoiceOrder), new SqlParameter("p_So_Ct0", ps_So_Ct0), new SqlParameter("p_So_Seri0", ps_So_Seri0), new SqlParameter("p_Check", null) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Bit;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckSoCt0";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = (bool) values[5].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return flag;
        }

        public bool Save(bool pb_InvoiceHistory, bool pb_NewChecked, string ps_Subsidiary, string ps_UserName, string ps_MasterInvoiceOrder, string ps_Loai_Ps, DataRow po_Row)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_SttV", po_Row["SttV"]), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Stt", ps_MasterInvoiceOrder), new SqlParameter("p_Ma_Ct", po_Row["Ma_Ct"]), new SqlParameter("p_Ngay_Ct", po_Row["Ngay_Ct"]), new SqlParameter("p_So_Ct", po_Row["So_Ct"]), new SqlParameter("p_Ngay_Ct0", po_Row["Ngay_Ct0"]), new SqlParameter("p_So_Ct0", po_Row["So_Ct0"]), new SqlParameter("p_So_Seri0", po_Row["So_Seri0"]), new SqlParameter("p_Mau_So0", po_Row["Mau_So0"]), new SqlParameter("p_Dien_Giai", po_Row["Dien_Giai"]), new SqlParameter("p_Ghi_Chu", po_Row["Ghi_Chu"]), new SqlParameter("p_Ma_Dt", po_Row["Ma_Dt"]), new SqlParameter("p_Ma_DtGTGT", po_Row["Ma_DtGTGT"]), new SqlParameter("p_Ten_DtGTGT", po_Row["Ten_DtGTGT"]), new SqlParameter("p_VAT_Ck", po_Row["VAT_Ck"]), 
                new SqlParameter("p_Thue_GTGT", po_Row["Thue_GTGT"]), new SqlParameter("p_Tk", po_Row["Tk"]), new SqlParameter("p_No_Co", po_Row["No_Co"]), new SqlParameter("p_Tk_Du", po_Row["Tk_Du"]), new SqlParameter("p_So_Luong", po_Row["So_Luong"]), new SqlParameter("p_Gia", po_Row["Gia"]), new SqlParameter("p_Tien", po_Row["Tien"]), new SqlParameter("p_Tien3", po_Row["Tien3"]), new SqlParameter("p_Hoa_Don", po_Row["Hoa_Don"]), new SqlParameter("p_Thue_XNK", po_Row["Thue_XNK"]), new SqlParameter("p_Hang_NLTS", po_Row["Hang_NLTS"]), new SqlParameter("p_TSCD_Ck", po_Row["TSCD_Ck"]), new SqlParameter("p_Dieu_Chinh", po_Row["Dieu_Chinh"]), new SqlParameter("p_Loai_VAT", po_Row["Loai_VAT"]), new SqlParameter("p_Ma_Sp", po_Row["Ma_Sp"]), new SqlParameter("p_Loai_Ps", ps_Loai_Ps), 
                new SqlParameter("p_UserName", po_Row["UserName"]), new SqlParameter("p_Posted", po_Row["Posted"]), new SqlParameter("p_Locked", po_Row["Locked"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x23].Direction = ParameterDirection.Output;
            values[0x23].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            SqlTransaction transaction = SSConnection.BeginTransaction();
            if (!pb_NewChecked && pb_InvoiceHistory)
            {
                this.SaveHistory(SSConnection, transaction, ps_UserName, po_Row["SttV"]);
            }
            try
            {
                command.CommandText = "CtVAT_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Transaction = transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                transaction.Commit();
                SSDataAccess.CloseConnection(SSConnection);
                po_Row.Table.AcceptChanges();
                flag = ((int) values[0x23].Value) == 1;
            }
            catch (Exception exception)
            {
                transaction.Rollback();
                SSDataAccess.CloseConnection(SSConnection);
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        private void SaveHistory(SqlConnection po_Connection, SqlTransaction po_Transaction, string ps_UserName, object ps_InvoiceOrder)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Save_Delete", 1), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_SttV", ps_InvoiceOrder) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtVAT_Log_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
        }

        public void So_Seri_Get(string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct, string ps_InvoiceOrder, ref object ps_So_Seri0, ref object ps_Mau_So0)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ma_Ct", ps_InvoiceCode), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_So_Seri", ps_So_Seri0), new SqlParameter("p_Mau_So0", ps_Mau_So0) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.NVarChar;
            values[4].Size = 20;
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.NVarChar;
            values[5].Size = 20;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "So_Seri_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                ps_So_Seri0 = values[4].Value;
                ps_Mau_So0 = values[5].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }
    }
}

